package control

import(
	"project06/common/message"
	"fmt"
	"encoding/json"
	"net"
	"project06/server/utils"
)

type MessageControl struct{
    
}

func (this *MessageControl) ServiceSmsMesProcess(mes *message.Message) {
	onlineUsers:=userMgr.GetAllonlineUser()
	var smsMes message.SmsMes
	err:=json.Unmarshal([]byte(mes.Data),&smsMes)
	if err !=nil{
		fmt.Println("反序列化群发消息失败，err",err)
		return
	}

	for i,v:=range onlineUsers{
		if i==smsMes.User.UserId{
			continue
		}
		this.sendMesToClient(&smsMes,v.Conn)
	}


}

func (this *MessageControl)sendMesToClient(mes *message.SmsMes,conn net.Conn){

	tf:=&utils.Transfer{
		Conn:conn,
	}
	var resMes message.Message
	resMes.Type=message.SmsResMesType
	data,err:=json.Marshal(mes)
	if err !=nil{
		fmt.Println("序列化群发消息失败，err",err)
	}
	resMes.Data=string(data)
	data,err=json.Marshal(resMes)
	if err !=nil{
		fmt.Println("序列化群发消息失败，err",err)
	}

	err=tf.WritePkg(data)
	if err !=nil{
		fmt.Println("群发消息失败，err",err)
	}

}